######################################################
#WA Analysis 2018 Models
# When Women Run, Voters Will Follow (Sometimes): 
##Examining the Mobilizing Effect of Female Candidates in the 2014 and 2018 Midterm Elections 
#By Safarpour, Wyckoff Gaynor, Rouse, and Swers #
######################################################

#clean the environment.
rm(list=ls()) 

#Load required packages.
library(arm)
library(plyr)
library(mvtnorm) 
library(stargazer)
library(stats)
library(Hmisc)
library(lme4) 
library(dplyr)

#Setwd.
setwd("/Users/ACS/Dropbox/When Women Run/Revision_PoliticalBehavior/R&R Part 2/Publication Docs/Replication Data and Code/")

#read in WA data.
WA<-read.csv("WA2018GeneralDataFinal.csv", 
             header = TRUE, stringsAsFactors = F)

length(WA$X) #( 4,759,368 obs)

#Model.
mainmodel<-glm(voted2018general~ womangeneral+ 
                 female+ womangeneral*female+
                 GenerationZ+ womangeneral*GenerationZ+
                 Millennials+ womangeneral*Millennials+
                 GenerationX+ womangeneral*GenerationX+ 
                 SilentGeneration+ womangeneral*SilentGeneration+ 
                 voted2016+trumpvote2016+totalreceipts, data=WA, 
               family=binomial(link="logit") )
mainmodel

stargazer(mainmodel, out="WA2018GeneralCompetitivenessControl.html",
          type="html",
          covariate.labels = c("Female Candidate", "Female", "Generation Z",
                               "Millennials", "Generation X","Silent Generation", 
                               "Voted in 2016 General Election", "County Trump Vote 2016", 
                               "Total Money Raised",
                               "Female Candidate*Female",
                               "Female Candidate*Generation Z",
                               "Female Candidate*Millennials",
                               "Female Candidate*Generation X",
                               "Female Candidate*Silent Generation"),
          dep.var.labels="Voted in WA 2018 General Election", column.labels = "",
          title = "Effects of candidate gender, generation, gender, prior voting, county Trump vote share, and total money raised on 2018 General Midterm Turnout in Washington",
          notes        = "Results from logistic regression. Standard errors in parentheses. Baseline age category: Baby Boomers.", 
          notes.append = TRUE, notes.align = "l", digits=3, single.row = T)

mainmodellog<-mainmodel

########################################
## Simulate CIs around predicted effects
########################################
n_draws <- 1000
set.seed(1714)
vcov<- vcov(mainmodellog)
coef<-coef(mainmodellog)
sim_coefs_mainmodellog <- rmvnorm(n_draws, coef, vcov) #Specify the 1,000 simulated coefficients.
rbind(coef(mainmodellog), apply(sim_coefs_mainmodellog, 2, mean)) # Check they are close to original

sample <- WA[complete.cases(WA$voted2018general, WA$womangeneral, WA$female,
                            WA$GenerationZ, WA$Millennials, WA$GenerationX, 
                            WA$SilentGeneration,
                            WA$voted2016, WA$trumpvote2016, WA$totalreceipts)==T,] # Regression sample

#Generate Predictions for male only candidates.
ppwomen0.s <- NULL
for (i in 1:n_draws) {
  ppwomen0.s[i] <- mean(invlogit(sim_coefs_mainmodellog[i,1] + 
                                   sim_coefs_mainmodellog[i,2]*0 + 
                                   sim_coefs_mainmodellog[i,3]*sample$female + 
                                   sim_coefs_mainmodellog[i,4]*sample$GenerationZ  +
                                   sim_coefs_mainmodellog[i,5]*sample$Millennials  +
                                   sim_coefs_mainmodellog[i,6]*sample$GenerationX +
                                   sim_coefs_mainmodellog[i,7]*sample$SilentGeneration +
                                   sim_coefs_mainmodellog[i,8]*sample$voted2016 +
                                   sim_coefs_mainmodellog[i,9]*sample$trumpvote2016 +
                                   sim_coefs_mainmodellog[i,10]*sample$totalreceipts +
                                   sim_coefs_mainmodellog[i,11]*0*sample$female+
                                   sim_coefs_mainmodellog[i,12]*0*sample$GenerationZ+
                                   sim_coefs_mainmodellog[i,13]*0*sample$Millennials+
                                   sim_coefs_mainmodellog[i,14]*0*sample$GenerationX+
                                   sim_coefs_mainmodellog[i,15]*0*sample$SilentGeneration))
}
mean(ppwomen0.s) #

#Generate Predictions for female only candidates.
ppwomen1.s <- NULL
for (i in 1:n_draws) {
  ppwomen1.s[i] <- mean(invlogit(sim_coefs_mainmodellog[i,1] + 
                                   sim_coefs_mainmodellog[i,2]*1 + 
                                   sim_coefs_mainmodellog[i,3]*sample$female + 
                                   sim_coefs_mainmodellog[i,4]*sample$GenerationZ  +
                                   sim_coefs_mainmodellog[i,5]*sample$Millennials  +
                                   sim_coefs_mainmodellog[i,6]*sample$GenerationX +
                                   sim_coefs_mainmodellog[i,7]*sample$SilentGeneration +
                                   sim_coefs_mainmodellog[i,8]*sample$voted2016 +
                                   sim_coefs_mainmodellog[i,9]*sample$trumpvote2016 +
                                   sim_coefs_mainmodellog[i,10]*sample$totalreceipts +
                                   sim_coefs_mainmodellog[i,11]*1*sample$female+
                                   sim_coefs_mainmodellog[i,12]*1*sample$GenerationZ+
                                   sim_coefs_mainmodellog[i,13]*1*sample$Millennials+
                                   sim_coefs_mainmodellog[i,14]*1*sample$GenerationX+
                                   sim_coefs_mainmodellog[i,15]*1*sample$SilentGeneration))
}
mean(ppwomen1.s) #


effect.01 <-  ppwomen1.s-ppwomen0.s
summary(effect.01) #  (0-1 women candidate effect)

# CIs and put results together in matrix
elements <- list(ppwomen0.s, ppwomen1.s, effect.01) # I split these two lines so the code is clear.
lapply(elements, summary) # See summary for each element of the list
ci2 <- do.call("rbind", (lapply(elements, quantile, c(.025,.975)))) # Get quantiles for each

results <- cbind(ci2[,1], lapply(elements, mean), ci2[,2])
colnames(results) <- c("2.5", "Mean", "97.5")   
rownames(results) <- c("Only Male Candidates", "Female Candidate", "Effect Male only-Female Candidate")
results

meaneffect<-results[[6]]
upperci<-results[[3]]
lowerci<-results[[9]]

##Get Predictions by Generation.
w0.SG	<- NULL
w1.SG 	<- NULL
w0.BB	<- NULL
w1.BB 	<- NULL
w0.GX	<- NULL
w1.GX 	<- NULL
w0.M	<- NULL
w1.M 	<- NULL
w0.GZ	<- NULL
w1.GZ 	<- NULL

SG <- subset(sample, SilentGeneration==1)
BB <- subset(sample, BabyBoomers==1)
GX <- subset(sample, GenerationX==1) 
M <- subset(sample, Millennials==1) 
GZ <- subset(sample, GenerationZ==1) 

for(i in 1:n_draws){ 
  w0.SG[i] <- mean(invlogit(sim_coefs_mainmodellog[i,1] + 
                              sim_coefs_mainmodellog[i,2]*0 + 
                              sim_coefs_mainmodellog[i,3]*SG$female + 
                              sim_coefs_mainmodellog[i,4]*SG$GenerationZ  +
                              sim_coefs_mainmodellog[i,5]*SG$Millennials  +
                              sim_coefs_mainmodellog[i,6]*SG$GenerationX +
                              sim_coefs_mainmodellog[i,7]*SG$SilentGeneration +
                              sim_coefs_mainmodellog[i,8]*SG$voted2016 +
                              sim_coefs_mainmodellog[i,9]*SG$trumpvote2016 +
                              sim_coefs_mainmodellog[i,10]*SG$totalreceipts +
                              sim_coefs_mainmodellog[i,11]*0*SG$female+
                              sim_coefs_mainmodellog[i,12]*0*SG$GenerationZ+
                              sim_coefs_mainmodellog[i,13]*0*SG$Millennials+
                              sim_coefs_mainmodellog[i,14]*0*SG$GenerationX+
                              sim_coefs_mainmodellog[i,15]*0*SG$SilentGeneration))
  w1.SG[i] <- mean(invlogit(sim_coefs_mainmodellog[i,1] + 
                              sim_coefs_mainmodellog[i,2]*1 + 
                              sim_coefs_mainmodellog[i,3]*SG$female + 
                              sim_coefs_mainmodellog[i,4]*SG$GenerationZ  +
                              sim_coefs_mainmodellog[i,5]*SG$Millennials  +
                              sim_coefs_mainmodellog[i,6]*SG$GenerationX +
                              sim_coefs_mainmodellog[i,7]*SG$SilentGeneration +
                              sim_coefs_mainmodellog[i,8]*SG$voted2016 +
                              sim_coefs_mainmodellog[i,9]*SG$trumpvote2016 +
                              sim_coefs_mainmodellog[i,10]*SG$totalreceipts +
                              sim_coefs_mainmodellog[i,11]*1*SG$female+
                              sim_coefs_mainmodellog[i,12]*1*SG$GenerationZ+
                              sim_coefs_mainmodellog[i,13]*1*SG$Millennials+
                              sim_coefs_mainmodellog[i,14]*1*SG$GenerationX+
                              sim_coefs_mainmodellog[i,15]*1*SG$SilentGeneration))
  w0.BB[i] <- mean(invlogit(sim_coefs_mainmodellog[i,1] + 
                              sim_coefs_mainmodellog[i,2]*0 + 
                              sim_coefs_mainmodellog[i,3]*BB$female + 
                              sim_coefs_mainmodellog[i,4]*BB$GenerationZ  +
                              sim_coefs_mainmodellog[i,5]*BB$Millennials  +
                              sim_coefs_mainmodellog[i,6]*BB$GenerationX +
                              sim_coefs_mainmodellog[i,7]*BB$SilentGeneration +
                              sim_coefs_mainmodellog[i,8]*BB$voted2016 +
                              sim_coefs_mainmodellog[i,9]*BB$trumpvote2016 +
                              sim_coefs_mainmodellog[i,10]*BB$totalreceipts +
                              sim_coefs_mainmodellog[i,11]*0*BB$female+
                              sim_coefs_mainmodellog[i,12]*0*BB$GenerationZ+
                              sim_coefs_mainmodellog[i,13]*0*BB$Millennials+
                              sim_coefs_mainmodellog[i,14]*0*BB$GenerationX+
                              sim_coefs_mainmodellog[i,15]*0*BB$SilentGeneration))
  w1.BB[i] <- mean(invlogit(sim_coefs_mainmodellog[i,1] + 
                              sim_coefs_mainmodellog[i,2]*1 + 
                              sim_coefs_mainmodellog[i,3]*BB$female + 
                              sim_coefs_mainmodellog[i,4]*BB$GenerationZ  +
                              sim_coefs_mainmodellog[i,5]*BB$Millennials  +
                              sim_coefs_mainmodellog[i,6]*BB$GenerationX +
                              sim_coefs_mainmodellog[i,7]*BB$SilentGeneration +
                              sim_coefs_mainmodellog[i,8]*BB$voted2016 +
                              sim_coefs_mainmodellog[i,9]*BB$trumpvote2016 +
                              sim_coefs_mainmodellog[i,10]*BB$totalreceipts +
                              sim_coefs_mainmodellog[i,11]*1*BB$female+
                              sim_coefs_mainmodellog[i,12]*1*BB$GenerationZ+
                              sim_coefs_mainmodellog[i,13]*1*BB$Millennials+
                              sim_coefs_mainmodellog[i,14]*1*BB$GenerationX+
                              sim_coefs_mainmodellog[i,15]*1*BB$SilentGeneration))
  w0.GX[i] <- mean(invlogit(sim_coefs_mainmodellog[i,1] + 
                              sim_coefs_mainmodellog[i,2]*0 + 
                              sim_coefs_mainmodellog[i,3]*GX$female + 
                              sim_coefs_mainmodellog[i,4]*GX$GenerationZ  +
                              sim_coefs_mainmodellog[i,5]*GX$Millennials  +
                              sim_coefs_mainmodellog[i,6]*GX$GenerationX +
                              sim_coefs_mainmodellog[i,7]*GX$SilentGeneration +
                              sim_coefs_mainmodellog[i,8]*GX$voted2016 +
                              sim_coefs_mainmodellog[i,9]*GX$trumpvote2016 +
                              sim_coefs_mainmodellog[i,10]*GX$totalreceipts +
                              sim_coefs_mainmodellog[i,11]*0*GX$female+
                              sim_coefs_mainmodellog[i,12]*0*GX$GenerationZ+
                              sim_coefs_mainmodellog[i,13]*0*GX$Millennials+
                              sim_coefs_mainmodellog[i,14]*0*GX$GenerationX+
                              sim_coefs_mainmodellog[i,15]*0*GX$SilentGeneration))
  w1.GX[i] <- mean(invlogit(sim_coefs_mainmodellog[i,1] + 
                              sim_coefs_mainmodellog[i,2]*1 + 
                              sim_coefs_mainmodellog[i,3]*GX$female + 
                              sim_coefs_mainmodellog[i,4]*GX$GenerationZ  +
                              sim_coefs_mainmodellog[i,5]*GX$Millennials  +
                              sim_coefs_mainmodellog[i,6]*GX$GenerationX +
                              sim_coefs_mainmodellog[i,7]*GX$SilentGeneration +
                              sim_coefs_mainmodellog[i,8]*GX$voted2016 +
                              sim_coefs_mainmodellog[i,9]*GX$trumpvote2016 +
                              sim_coefs_mainmodellog[i,10]*GX$totalreceipts +
                              sim_coefs_mainmodellog[i,11]*1*GX$female+
                              sim_coefs_mainmodellog[i,12]*1*GX$GenerationZ+
                              sim_coefs_mainmodellog[i,13]*1*GX$Millennials+
                              sim_coefs_mainmodellog[i,14]*1*GX$GenerationX+
                              sim_coefs_mainmodellog[i,15]*1*GX$SilentGeneration))
  w0.M[i] <- mean(invlogit(sim_coefs_mainmodellog[i,1] + 
                             sim_coefs_mainmodellog[i,2]*0 + 
                             sim_coefs_mainmodellog[i,3]*M$female + 
                             sim_coefs_mainmodellog[i,4]*M$GenerationZ  +
                             sim_coefs_mainmodellog[i,5]*M$Millennials  +
                             sim_coefs_mainmodellog[i,6]*M$GenerationX +
                             sim_coefs_mainmodellog[i,7]*M$SilentGeneration +
                             sim_coefs_mainmodellog[i,8]*M$voted2016 +
                             sim_coefs_mainmodellog[i,9]*M$trumpvote2016 +
                             sim_coefs_mainmodellog[i,10]*M$totalreceipts +
                             sim_coefs_mainmodellog[i,11]*0*M$female+
                             sim_coefs_mainmodellog[i,12]*0*M$GenerationZ+
                             sim_coefs_mainmodellog[i,13]*0*M$Millennials+
                             sim_coefs_mainmodellog[i,14]*0*M$GenerationX+
                             sim_coefs_mainmodellog[i,15]*0*M$SilentGeneration))
  w1.M[i] <- mean(invlogit(sim_coefs_mainmodellog[i,1] + 
                             sim_coefs_mainmodellog[i,2]*1 + 
                             sim_coefs_mainmodellog[i,3]*M$female + 
                             sim_coefs_mainmodellog[i,4]*M$GenerationZ  +
                             sim_coefs_mainmodellog[i,5]*M$Millennials  +
                             sim_coefs_mainmodellog[i,6]*M$GenerationX +
                             sim_coefs_mainmodellog[i,7]*M$SilentGeneration +
                             sim_coefs_mainmodellog[i,8]*M$voted2016 +
                             sim_coefs_mainmodellog[i,9]*M$trumpvote2016 +
                             sim_coefs_mainmodellog[i,10]*M$totalreceipts +
                             sim_coefs_mainmodellog[i,11]*1*M$female+
                             sim_coefs_mainmodellog[i,12]*1*M$GenerationZ+
                             sim_coefs_mainmodellog[i,13]*1*M$Millennials+
                             sim_coefs_mainmodellog[i,14]*1*M$GenerationX+
                             sim_coefs_mainmodellog[i,15]*1*M$SilentGeneration))
  w0.GZ[i] <- mean(invlogit(sim_coefs_mainmodellog[i,1] + 
                              sim_coefs_mainmodellog[i,2]*0 + 
                              sim_coefs_mainmodellog[i,3]*GZ$female + 
                              sim_coefs_mainmodellog[i,4]*GZ$GenerationZ  +
                              sim_coefs_mainmodellog[i,5]*GZ$Millennials  +
                              sim_coefs_mainmodellog[i,6]*GZ$GenerationX +
                              sim_coefs_mainmodellog[i,7]*GZ$SilentGeneration +
                              sim_coefs_mainmodellog[i,8]*GZ$voted2016 +
                              sim_coefs_mainmodellog[i,9]*GZ$trumpvote2016 +
                              sim_coefs_mainmodellog[i,10]*GZ$totalreceipts +
                              sim_coefs_mainmodellog[i,11]*0*GZ$female+
                              sim_coefs_mainmodellog[i,12]*0*GZ$GenerationZ+
                              sim_coefs_mainmodellog[i,13]*0*GZ$Millennials+
                              sim_coefs_mainmodellog[i,14]*0*GZ$GenerationX+
                              sim_coefs_mainmodellog[i,15]*0*GZ$SilentGeneration))
  w1.GZ[i] <- mean(invlogit(sim_coefs_mainmodellog[i,1] + 
                              sim_coefs_mainmodellog[i,2]*1 + 
                              sim_coefs_mainmodellog[i,3]*GZ$female + 
                              sim_coefs_mainmodellog[i,4]*GZ$GenerationZ  +
                              sim_coefs_mainmodellog[i,5]*GZ$Millennials  +
                              sim_coefs_mainmodellog[i,6]*GZ$GenerationX +
                              sim_coefs_mainmodellog[i,7]*GZ$SilentGeneration +
                              sim_coefs_mainmodellog[i,8]*GZ$voted2016 +
                              sim_coefs_mainmodellog[i,9]*GZ$trumpvote2016 +
                              sim_coefs_mainmodellog[i,10]*GZ$totalreceipts +
                              sim_coefs_mainmodellog[i,11]*1*GZ$female+
                              sim_coefs_mainmodellog[i,12]*1*GZ$GenerationZ+
                              sim_coefs_mainmodellog[i,13]*1*GZ$Millennials+
                              sim_coefs_mainmodellog[i,14]*1*GZ$GenerationX+
                              sim_coefs_mainmodellog[i,15]*1*GZ$SilentGeneration))
}

effect.SG	<- w1.SG - w0.SG 
quantile(effect.SG, c(.025,.975)) #to get CIs; 
mean(effect.SG) #mean effect

effect.BB	<-  w1.BB - w0.BB
quantile(effect.BB, c(.025,.975)) #to get CIs;  
mean(effect.BB)  

effect.GX	<-  w1.GX - w0.GX
quantile(effect.GX, c(.025,.975)) #to get CIs;  
mean(effect.GX) 

effect.M	<-  w1.M - w0.M
quantile(effect.M, c(.025,.975)) #to get CIs; 
mean(effect.M)  

effect.GZ	<-  w1.GZ - w0.GZ
quantile(effect.GZ, c(.025,.975)) #to get CIs;  
mean(effect.GZ) 

#Effect by gender.
w0.F	<- NULL
w1.F 	<- NULL
w0.M	<- NULL
w1.M 	<- NULL

Men <- subset(sample, female==0)
Women <- subset(sample, female==1)

for(i in 1:n_draws){ 
  w0.F[i] <- mean(invlogit(sim_coefs_mainmodellog[i,1] + 
                             sim_coefs_mainmodellog[i,2]*0 + 
                             sim_coefs_mainmodellog[i,3]*Women$female + 
                             sim_coefs_mainmodellog[i,4]*Women$GenerationZ +
                             sim_coefs_mainmodellog[i,5]*Women$Millennials  +
                             sim_coefs_mainmodellog[i,6]*Women$GenerationX +
                             sim_coefs_mainmodellog[i,7]*Women$SilentGeneration +
                             sim_coefs_mainmodellog[i,8]*Women$voted2016 +
                             sim_coefs_mainmodellog[i,9]*Women$trumpvote2016 +
                             sim_coefs_mainmodellog[i,10]*Women$totalreceipts +
                             sim_coefs_mainmodellog[i,11]*Women$female*0+
                             sim_coefs_mainmodellog[i,12]*0*Women$GenerationZ+
                             sim_coefs_mainmodellog[i,13]*0*Women$Millennials+
                             sim_coefs_mainmodellog[i,14]*0*Women$GenerationX+
                             sim_coefs_mainmodellog[i,15]*0*Women$SilentGeneration))
  w1.F[i] <- mean(invlogit(sim_coefs_mainmodellog[i,1] + 
                             sim_coefs_mainmodellog[i,2]*1 + 
                             sim_coefs_mainmodellog[i,3]*Women$female + 
                             sim_coefs_mainmodellog[i,4]*Women$GenerationZ +
                             sim_coefs_mainmodellog[i,5]*Women$Millennials  +
                             sim_coefs_mainmodellog[i,6]*Women$GenerationX +
                             sim_coefs_mainmodellog[i,7]*Women$SilentGeneration +
                             sim_coefs_mainmodellog[i,8]*Women$voted2016 +
                             sim_coefs_mainmodellog[i,9]*Women$trumpvote2016 +
                             sim_coefs_mainmodellog[i,10]*Women$totalreceipts +
                             sim_coefs_mainmodellog[i,11]*Women$female*1+
                             sim_coefs_mainmodellog[i,12]*1*Women$GenerationZ+
                             sim_coefs_mainmodellog[i,13]*1*Women$Millennials+
                             sim_coefs_mainmodellog[i,14]*1*Women$GenerationX+
                             sim_coefs_mainmodellog[i,15]*1*Women$SilentGeneration))
  w0.M[i] <- mean(invlogit(sim_coefs_mainmodellog[i,1] + 
                             sim_coefs_mainmodellog[i,2]*0 + 
                             sim_coefs_mainmodellog[i,3]*Men$female + 
                             sim_coefs_mainmodellog[i,4]*Men$GenerationZ +
                             sim_coefs_mainmodellog[i,5]*Men$Millennials  +
                             sim_coefs_mainmodellog[i,6]*Men$GenerationX +
                             sim_coefs_mainmodellog[i,7]*Men$SilentGeneration +
                             sim_coefs_mainmodellog[i,8]*Men$voted2016 +
                             sim_coefs_mainmodellog[i,9]*Men$trumpvote2016 +
                             sim_coefs_mainmodellog[i,10]*Men$totalreceipts +
                             sim_coefs_mainmodellog[i,11]*Men$female*0+
                             sim_coefs_mainmodellog[i,12]*0*Men$GenerationZ +
                             sim_coefs_mainmodellog[i,13]*0*Men$Millennials+
                             sim_coefs_mainmodellog[i,14]*0*Men$GenerationX+
                             sim_coefs_mainmodellog[i,15]*0*Men$SilentGeneration))
  w1.M[i] <- mean(invlogit(sim_coefs_mainmodellog[i,1] + 
                             sim_coefs_mainmodellog[i,2]*1 + 
                             sim_coefs_mainmodellog[i,3]*Men$female + 
                             sim_coefs_mainmodellog[i,4]*Men$GenerationZ +
                             sim_coefs_mainmodellog[i,5]*Men$Millennials  +
                             sim_coefs_mainmodellog[i,6]*Men$GenerationX +
                             sim_coefs_mainmodellog[i,7]*Men$SilentGeneration +
                             sim_coefs_mainmodellog[i,8]*Men$voted2016 +
                             sim_coefs_mainmodellog[i,9]*Men$trumpvote2016 +
                             sim_coefs_mainmodellog[i,10]*Men$totalreceipts +
                             sim_coefs_mainmodellog[i,11]*Men$female*1+
                             sim_coefs_mainmodellog[i,12]*1*Men$GenerationZ+
                             sim_coefs_mainmodellog[i,13]*1*Men$Millennials+
                             sim_coefs_mainmodellog[i,14]*1*Men$GenerationX+
                             sim_coefs_mainmodellog[i,15]*1*Men$SilentGeneration))
}

effect.Men	<-  w1.M - w0.M
quantile(effect.Men, c(.025,.975)) #to get CIs;  
mean(effect.Men)  

effect.Women	<-  w1.F - w0.F
quantile(effect.Women, c(.025,.975)) #to get CIs;  
mean(effect.Women)   

#Store the Effects.
Election<-c(rep("2018 General",8))
Effect<-c(meaneffect, mean(effect.Women), mean(effect.Men), mean(effect.GZ),mean(effect.M),mean(effect.GX),mean(effect.BB),mean(effect.SG))
LowerCI<-c(lowerci,  quantile(effect.Women, c(.025)), quantile(effect.Men, c(.025)), quantile(effect.GZ, c(.025)),quantile(effect.M, c(.025)),quantile(effect.GX, c(.025)),quantile(effect.BB, c(.025)),quantile(effect.SG, c(.025)))
UpperCI<-c(upperci, quantile(effect.Women, c(.975)), quantile(effect.Men, c(.975)), quantile(effect.GZ, c(.975)),quantile(effect.M, c(.975)),quantile(effect.GX, c(.975)),quantile(effect.BB, c(.975)),quantile(effect.SG, c(.975)))
WAgeneral<-data.frame(Effect, UpperCI, LowerCI, Election) 
WAgeneral$`Registrant Subgroup`<-factor(c(1,2,3,4,5,6,7,8), levels=c(1,2,3,4,5,6,7,8),
                                        labels=c("Overall", "Women", "Men", "Gen. Z", "Millennials", "Gen. X", "Baby\nBoomers", "Silent Gen."), ordered = T)
#write effects to data frame.
write.csv(WAgeneral, "WA2018GeneralCompetitivenessControl.csv", row.names=F)


